package com.zsck.zsgy.ui.guide

import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.zsck.zsgy.R
import com.zsck.zsgy.base.BaseActivity
import com.zsck.zsgy.base.BaseViewModel
import com.zsck.zsgy.databinding.ActivityGuideLayoutBinding
import com.zsck.zsgy.ui.main.MainActivity
import com.zsck.zsgy.util.PrefserHelper

class GuideActivity : BaseActivity<BaseViewModel, ActivityGuideLayoutBinding>() {


    override fun layoutId(): Int {
        return R.layout.activity_guide_layout
    }

    var mList: ArrayList<View>? = ArrayList()
    private var curPos = 0
    override fun initView() {
        super.initView()
        var view1 = View.inflate(this, R.layout.guid_view1, null)
        mList!!.add(view1)
        var view2 = View.inflate(this, R.layout.guid_view2, null)
        mList!!.add(view2)
        var view3 = View.inflate(this, R.layout.guid_view3, null)
        mList!!.add(view3)
        binding.viewpager.adapter = GuidePagerAdapter()
        binding.viewpager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                curPos = position
                if (position == 2) {
                    binding.jump.setVisibility(View.VISIBLE)
                } else {
                    binding.jump.setVisibility(View.GONE)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
        binding.jump.setOnClickListener(View.OnClickListener {
            if (curPos == 2) {
                startActivity(SplashActivity::class.java)
                finish()
            }
        })
    }


    inner class GuidePagerAdapter : PagerAdapter() {
        override fun getCount(): Int {
            return mList!!.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            (container as ViewPager).addView(mList!![position])
            return mList!![position]
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            (container as ViewPager).removeView(mList!![position])
        }

    }

    override fun onPause() {
        super.onPause()
        PrefserHelper.setFirst(true)
    }

    override fun onDestroy() {
        super.onDestroy()
        PrefserHelper.setFirst(true)
    }

}